var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "categoryId",
            pIdKey: "parentCategoryId",
            rootPId: -1
        },
        key: {
            name: "categoryName",
            url:"nourl"
        }
    },
    height: 800,
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        goodsCategory:{
            parentName:null,
            parentCategoryId:0,
            categoryOrder:0,
            imgId:'',
            delImage:''
        },
        //分类关联热门商品表
        goodsCategoryRelation:{},
        //商品模板表
        goodsTemplateList:{},
        goodsCategoryEntityList:[],
        goodsTypeEnum:[],
        //分类图片上传容器
        goodsImageEntity:{},
        image:{},
        node:{},
    },
    methods: {
        getGoodsCategory: function(){
            //加载商品分类树
            $.get(baseURL + "goodsCategory/goodscategory/select", function(r){
                ztree = $.fn.zTree.init($("#goodsCategoryTree"), setting, r.goodsCategoryEntityList);
                ;
                var node = ztree.getNodeByParam("categoryId", vm.goodsCategory.parentCategoryId);
                ztree.selectNode(node);
                vm.goodsCategory.parentName = node.categoryName;
            })
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.goodsCategory = {parentName:null,parentCategoryId:0,categoryOrder:0,imgId:''};
            //分类关联热门商品表
            vm.goodsCategoryRelation={};
            //商品模板表
            vm.goodsTemplateList={};
            vm.goodsCategoryEntityList=[];
                //分类图片容器
            vm.goodsImageEntity={};
            vm.image={};
            vm.getGoodsCategory();
        },
        update: function (event) {
            var categoryId = getGoodsCategoryId();
            if(categoryId == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.goodsCategory = {parentName:null,parentCategoryId:0,categoryOrder:0,imgId:''};
            //分类关联热门商品表
            vm.goodsCategoryRelation={};
            //商品模板表
            vm.goodsTemplateList={};
            vm.goodsCategoryEntityList=[],
                //分类图片容器
            vm.goodsImageEntity={};
            vm.image={}
            $.get(baseURL + "goodsCategory/goodscategory/info/"+categoryId, function(r){
                debugger;
                vm.goodsCategory = r.goodsCategory;
                console.info(vm.goodsCategory.categoryOrder)
                if(!isBlank(r.goodsCategoryRelation)){
                    vm.goodsCategoryRelation=r.goodsCategoryRelation;
                }
                // vm.goodsTemplateList=r.goodsTemplateList;
                if(!isBlank(r.goodsImageEntity)){
                    vm.image = r.goodsImageEntity;
                }
                vm.getGoodsCategory();
            });

        },
        saveOrUpdate: function (event) {
            //校验
            if(vm.validator()){
                return ;
            }
            var url = vm.goodsCategory.categoryId == null ? "goodsCategory/goodscategory/save" : "goodsCategory/goodscategory/update";
            vm.goodsCategory.goodsCategoryRelation = vm.goodsCategoryRelation;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.goodsCategory),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var categoryId = getGoodsCategoryId();
            if(categoryId == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "goodsCategory/goodscategory/delete",
                    contentType: "application/json",
                    data: categoryId,
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                // $("#jqGrid").trigger("reloadGrid");
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        hide:function(){
            var categoryId = getGoodsCategoryId();
            if(categoryId == null){
                return ;
            }
            $.get(baseURL + "goodsCategory/goodscategory/info/"+categoryId, function(r){
                vm.goodsCategory = r.goodsCategory;
                debugger;
                if(vm.goodsCategory.categoryLevel != 3){
                    alert("屏蔽功能只对三级分类开放!")
                    return;
                }
                if(vm.goodsCategory.hideFlag){
                    confirm('确定要在前端商品展示这个分类？', function(){
                        $.ajax({
                            type: "POST",
                            url: baseURL + "goodsCategory/goodscategory/hide/0",
                            contentType: "application/json",
                            data: categoryId,
                            success: function(r){
                                if(r.code == 0){
                                    alert('操作成功', function(index){
                                        vm.reload();
                                    });
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    });
                }else{
                    confirm('确定要在前端商品隐藏这个分类？', function(){
                        $.ajax({
                            type: "POST",
                            url: baseURL + "goodsCategory/goodscategory/hide/1",
                            contentType: "application/json",
                            data: categoryId,
                            success: function(r){
                                if(r.code == 0){
                                    alert('操作成功', function(index){
                                        vm.reload();
                                    });
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    });
                }
            });
        },
        goodsCategoryTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择商品分类",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#goodsCategoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.goodsCategory.parentCategoryId = node[0].categoryId;
                    vm.goodsCategory.parentName = node[0].categoryName;
                    if(vm.goodsCategory.categoryId == undefined && node[0].categoryLevel == 3){
                        //新建,判断三级分类
                        alert("三级分类不允许新建子分类")
                        return;
                    }
                    vm.node = node[0];
                    layer.close(index);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            GoodsCategory.table.refresh();
        },
        // 修改界面点保存
        // updateCategory: function (event) {
        //     $.ajax({
        //         type: "POST",
        //         url: baseURL + "goodsCategory/goodscategory/updateCategory",
        //         dataType: "json",
        //         data: $("#updateForm").serialize(),
        //         success: function(r){
        //             if(r.code === 0){
        //                 alert('操作成功', function(){
        //                     location.reload();
        //                 });
        //             }else{
        //                 alert(r.msg);
        //             }
        //         }
        //     });
        // },
        //修改界面删除图片
        delImg: function (id) {
            vm.image = {};
            vm.goodsCategory.imgId = '';
            // delImage
            vm.goodsCategory.delImage =  vm.goodsCategory.delImage + ","+id;
        },
        validator:function(){
            //分类校验
            //goodsCategory.parentCategoryId
            if(isBlank(vm.goodsCategory.parentCategoryId) && isBlank(vm.goodsCategory.categoryId)){
                alert("父分类不能为空!")
                return true;
            }
            if(isBlank(vm.goodsCategory.categoryName)){
                alert("分类名不能为空!")
                return true;
            }
            if(vm.goodsCategory.stageType == null || vm.goodsCategory.stageType == undefined){
                alert("分类展示平台不能为空!")
                return true;
            }
            if(vm.goodsCategory.categoryLevel == 3 && isBlank(vm.goodsCategory.goodsType)){
                alert("分类类型不能为空!")
                return true;
            }
            // if(isBlank(vm.goodsCategory.selfPaper)){
            //     alert("自来纸类型必须选择!")
            //     return true;
            // }
            // if(isBlank(vm.goodsCategory.canUploadFile)){
            //     alert("请选择是否允许上传!")
            //     return true;
            // }
        }
    }
});

var GoodsCategory = {
    id: "goodsCategoryTable",
    table: null,
    layerIndex: -1
};


/**
 * 初始化表格的列
 */
GoodsCategory.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '分类Id', field: 'categoryId', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '分类名称', field: 'categoryName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {
            title: '分类类型', field: 'goodsType', align: 'center', valign: 'middle', sortable: true, width: '180px',
            formatter: function (value, options, row) {
                if (value.goodsType == 'standard') {
                    return '标准商品';
                } else if (value.goodsType == 'print') {
                    return '印刷商品';
                } else if (value.goodsType == 'paper') {
                    return '大纸商品';
                } else if (value.goodsType == 'fast') {
                    return '快印商品';
                }else if (value.goodsType == 'material') {
                    return '广告物料';
                }else if (value.goodsType == 'copy') {
                    return '复印纸';
                }else if (value.goodsType == 'design') {
                    return '设计商品';
                }
            }
        },
        {title: '显示平台', field: 'stageType', align: 'center', valign: 'middle', sortable: true, width: '180px',
            formatter:function(value){
                if(value.stageType == 0){
                    return '紫云网、pop平台';
                }else if(value.stageType == 1){
                    return '紫云网';
                }
                else if(value.stageType == 2){
                    return 'pop平台';
                }
            }
        },
        // {title: '新旧数据标识(1为旧数据,0为新数据)', field: 'dateFlag', align: 'center', valign: 'middle', sortable: true, width: '180px',
        //     formatter:function(value){
        //         if(value.dateFlag == 0){
        //             return '新数据';
        //         }else if(value.dateFlag == 1){
        //             return '旧数据';
        //         }
        //     }
        // },
        // {title: '是否删除标识', field: 'disabled', align: 'center', valign: 'middle', sortable: true, width: '180px',
        //     formatter:function(value){
        //         if(value.disabled == 0){
        //             return '未删除';
        //         }else if(value.disabled == 1){
        //             return '已删除';
        //         }
        //     }
        // },
        // template_ids
        // {title: '上级部门', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        // {title: '排序号', field: 'categoryOrder', align: 'center', valign: 'middle', sortable: true, width: '100px'}
    ]
    return columns;
};



function getGoodsCategoryId () {
    var selected = $('#goodsCategoryTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}

$(function () {
    debugger;
    // $.get(baseURL + "goodsCategory/goodscategory/info", function(r){
    var colunms = GoodsCategory.initColumn();
    var table = new TreeTable(GoodsCategory.id, baseURL + "goodsCategory/goodscategory/list", colunms);
    // table.setRootCodeValue(r.categoryIid);
    table.setExpandColumn(2);
    table.setIdField("categoryId");
    table.setCodeField("categoryId");
    table.setParentCodeField("parentCategoryId");
    table.setExpandAll(false);
    table.init();
    GoodsCategory.table = table;
    // });

       //获取商品类型枚举
        $.get(baseURL + "goodsCategory/goodscategory/getGoodsType", function(r){
            vm.goodsTypeEnum = r.goodsTypeEnum;
        });
        /* oss上传 */
        $.files_up_oss({
            id: ["upload1"],//按钮id
            url: "/sys/oss/getSignature",
            dir: "erp/goods/categoryImages/",
            file_type: "jpg,png,gif,psd,ai,cdr,eps,ppt,word,excel,pdf,tiff,rar,zip,7z",
            size: "100mb",//mb
            view_url: window.parent.vm.ossHost
        }, function (index_number, json) {
            // console.info(json);
            if (json.Status == 'OK'){
                //分类图片
                vm.goodsImageEntity.imageUrl = json.filename;
                vm.goodsImageEntity.type = 2;
                if(!isBlank(vm.goodsCategory.categoryId)){
                    vm.goodsImageEntity.relationId = vm.goodsCategory.categoryId;
                }
                $.ajax({
                    type: "POST",
                    url: baseURL + "goodsImage/goodsimage/saveImage",
                    contentType: "application/json",
                    data: JSON.stringify(vm.goodsImageEntity),
                    success: function (r) {
                            if (r.code === 0) {
                            //回调成功
                            vm.goodsCategory.imgId = r.id;
                            vm.image = {"imageUrl": json.filename, "id": r.id};
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }else {

            }
        });
});

